package com.ysd.lis.service.callNum.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.callNum.CallSampleType;
import com.ysd.lis.entity.callNum.CallSampleVsFeeOrSam;
import com.ysd.lis.mapper.callNum.CallSampleTypeMapper;
import com.ysd.lis.service.callNum.CallSampleTypeService;
import com.ysd.lis.service.callNum.CallSampleVsFeeOrSamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wh
 * @since 2025-05-28
 */
@Service
public class CallSampleTypeServiceImpl extends ServiceImpl<CallSampleTypeMapper, CallSampleType> implements CallSampleTypeService {

    @Autowired
    private CallSampleVsFeeOrSamService  callSampleVsFeeOrSamService;
    @Override
    public Result findSampleTypeList(String seachValue) {
        LambdaQueryWrapper<CallSampleType> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(seachValue)) {
            queryWrapper.and(wrapper -> wrapper.like(CallSampleType::getSampleTypeCode, seachValue).or().like(CallSampleType::getSampleTypeName, seachValue));
        }
        queryWrapper.eq(CallSampleType::getDelFlag, 0);
        queryWrapper.orderByAsc(CallSampleType::getSeq);
        List<CallSampleType> list = this.list(queryWrapper);
        return Result.succ(1, "查询成功", list);
    }

    @Override
    public Result saveOrUpdateSampleType(List<CallSampleType> callSampleTypes) {
        boolean b = this.saveOrUpdateBatch(callSampleTypes);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result batchDeleteSampleType(List<CallSampleType> callSampleTypes) {
        List<String> ids = callSampleTypes.stream().map(CallSampleType::getId).collect(Collectors.toList());
        List<String> sampleCodeList = callSampleTypes.stream().map(CallSampleType::getSampleTypeCode).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            callSampleVsFeeOrSamService.remove(new LambdaQueryWrapper<CallSampleVsFeeOrSam>().in(CallSampleVsFeeOrSam::getSampleTypeCode, sampleCodeList));
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }
}
